import pymongo
from datetime import datetime
import time


#将log中的空行去掉
# def file_replace():
#     with open('logs.txt', 'r', encoding='utf-8') as file:
#         with open('log.txt', 'w', encoding='utf-8') as file1:
#             for line in file.readlines():
#                 if line == '\n':
#                     line = line.strip("\n")
#                 file1.write(line)


#将所有数据读取出来进行去除空格和切片
c = []
with open('log.txt','r',encoding='UTF-8-sig') as file:
        a = file.readlines()
        for i in a:
            i = i.strip()
            b = i.strip().split()
            f = b[1].split(',')[0]
            d = b[1].split(',')[1]
            b[1]=f
            c.append(b)
test_date = []
# 将数据写入到MongoDB数据中
for j in c:
    myclient = pymongo.MongoClient("mongodb://dl.izhaohu.com:27017/")
    mydb = myclient['iot']
    mycol = mydb['ibed_testcase_log']
    dtime = j[0] + ' ' + j[1]
    mydict = {'ft': j[0] + ' ' + j[1], 'Message': j[9],
              'reader_id': '60001B','flg':j[9][5:8],'flg_category':j[9][9:12],
              'l':int(time.mktime(time.strptime(dtime, '%Y-%m-%d %H:%M:%S'))*1000+int(d))}
    test_date.append(mydict)
x = mycol.insert_many(test_date)
print(x.inserted_ids)


#删除错误的log
myclient = pymongo.MongoClient("mongodb://dl.izhaohu.com:27017/")
mydb = myclient['iot']
mycol = mydb['ibed_testcase_log']
myquery = { "flg": "出错啦"}
x = mycol.delete_many(myquery)  #批量删除
print(x.deleted_count)  #返回删除个数
